<template>
  <div>
    <van-picker
      show-toolbar
      :columns="columns"
      @cancel="$emit('close')"
      :default-index="value"
      @confirm="submit"
      @change="onChange"
    />
  </div>
</template>

<script>
import {editUserInfoAPI} from '@/api'
export default {
  props:{
    value:{
      type: Number,
      required: true
    }
  },
data(){
  return {
    columns: ['男','女'],
    index: this.gender
  }
},
methods: {
  submit(){
    this.editUserInfo()
  },
  async editUserInfo(){
      this.$toast.loading({
        duration: 0, // 持续展示 toast
        forbidClick: true,
        message: '保存中...',
      })
      try {
        await editUserInfoAPI({
          gender: this.index
        })
        this.$emit('input',this.index)
        this.$emit('close')
        this.$toast.success('保存成功')
      } catch (error) {
        this.$toast.fail('操作失败，请稍后重试！')
      }
  },
  //获取发生改变时的索引值
  onChange(picker, value, index) {
      this.index = index
    },
}
}
</script>

<style>

</style>